'use strict';

const Service = require('egg').Service;
class User extends Service {
    
    async getUserName(userId){
        let list = await this.getUserList();

        let name = "";

        for(let i in list){
            if(list[i]['id'] == userId){
                name = list[i]['nick_name'];
                break;
            }
        }
        
        return name;
    }

    async getUserList(){
        const { ctx } = this;
        let ck = "cache_userList";
        let list = await ctx.service.cache.get(ck);

        if(!list){
            list = await ctx.model.User.findAll();

            await ctx.service.cache.set(ck, list, 120);
        }

        return list;
    }
}

module.exports = User;